home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap09 / Paint7 / Paint7Doc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  3.1 KB  |  132 lines

  1. //***********************************************************************
  2. //
  3. //  Paint7Doc.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "Resource.h"
  9. #include "CLine.h"
  10. #include "Paint7Doc.h"
  11.  
  12. IMPLEMENT_DYNCREATE (CPaintDoc, CDocument)
  13.  
  14. BEGIN_MESSAGE_MAP (CPaintDoc, CDocument)
  15.     ON_COMMAND (ID_FILE_SEND_MAIL, OnFileSendMail)
  16.     ON_COMMAND_RANGE (ID_WIDTH_VTHIN, ID_WIDTH_VTHICK, OnWidth)
  17.     ON_COMMAND_RANGE (ID_COLOR_BLACK, ID_COLOR_WHITE, OnColor)
  18.     ON_UPDATE_COMMAND_UI (ID_FILE_SEND_MAIL, OnUpdateFileSendMail)
  19.     ON_UPDATE_COMMAND_UI_RANGE (ID_WIDTH_VTHIN, ID_WIDTH_VTHICK,
  20.         OnUpdateWidthUI)
  21.     ON_UPDATE_COMMAND_UI_RANGE (ID_COLOR_BLACK, ID_COLOR_WHITE,
  22.         OnUpdateColorUI)
  23. END_MESSAGE_MAP ()
  24.  
  25. const COLORREF CPaintDoc::m_crColors[8] = {
  26.     RGB (  0,   0,   0),    // Black
  27.     RGB (  0,   0, 255),    // Blue
  28.     RGB (  0, 255,   0),    // Green
  29.     RGB (  0, 255, 255),    // Cyan
  30.     RGB (255,   0,   0),    // Red
  31.     RGB (255,   0, 255),    // Magenta
  32.     RGB (255, 255,   0),    // Yellow
  33.     RGB (255, 255, 255)     // White
  34. };
  35.  
  36. CPaintDoc::CPaintDoc ()
  37. {
  38.     m_lineArray.SetSize (0, 64);
  39. }
  40.  
  41. BOOL CPaintDoc::OnNewDocument ()
  42. {
  43.     if (!CDocument::OnNewDocument ())
  44.         return FALSE;
  45.  
  46.     InitWidthAndColor ();
  47.     return TRUE;
  48. }
  49.  
  50. BOOL CPaintDoc::OnOpenDocument (LPCTSTR lpszPathName)
  51. {
  52.     if (!CDocument::OnOpenDocument (lpszPathName))
  53.         return FALSE;
  54.  
  55.     InitWidthAndColor ();
  56.     return TRUE;
  57. }
  58.  
  59. void CPaintDoc::InitWidthAndColor ()
  60. {
  61.     m_nColor = ID_COLOR_RED - ID_COLOR_BLACK;
  62.     m_nWidth = ID_WIDTH_MEDIUM - ID_WIDTH_VTHIN;
  63. }
  64.  
  65. void CPaintDoc::DeleteContents ()
  66. {
  67.     int nCount = m_lineArray.GetSize ();
  68.  
  69.     if (nCount) {
  70.         for (int i=0; i<nCount; i++)
  71.             delete m_lineArray[i];
  72.         m_lineArray.RemoveAll ();
  73.     }
  74. }
  75.  
  76. void CPaintDoc::Serialize (CArchive& ar)
  77. {
  78.     m_lineArray.Serialize (ar);
  79. }
  80.  
  81. CLine* CPaintDoc::AddLine (CPoint ptFrom, CPoint ptTo)
  82. {
  83.     static UINT nWidths[5] = { 1, 8, 16, 24, 32 };
  84.  
  85.     CLine* pLine;
  86.     try {
  87.         pLine = new CLine (ptFrom, ptTo, nWidths[m_nWidth],
  88.             m_crColors[m_nColor]);
  89.         m_lineArray.Add (pLine);
  90.         SetModifiedFlag ();
  91.     }
  92.     catch (CMemoryException* e) {
  93.         if (pLine != NULL) {
  94.             delete pLine;
  95.             pLine = NULL;
  96.         }
  97.         AfxMessageBox ("Out of memory", MB_ICONSTOP | MB_OK);
  98.         e->Delete ();
  99.     }
  100.     return pLine;
  101. }
  102.  
  103. CLine* CPaintDoc::GetLine (int nIndex)
  104. {
  105.     return (CLine*) m_lineArray[nIndex];
  106. }
  107.  
  108. int CPaintDoc::GetLineCount ()
  109. {
  110.     return m_lineArray.GetSize ();
  111. }
  112.  
  113. void CPaintDoc::OnWidth (UINT nID)
  114. {
  115.     m_nWidth = nID - ID_WIDTH_VTHIN;
  116. }
  117.  
  118. void CPaintDoc::OnColor (UINT nID)
  119. {
  120.     m_nColor = nID - ID_COLOR_BLACK;
  121. }
  122.  
  123. void CPaintDoc::OnUpdateWidthUI (CCmdUI* pCmdUI)
  124. {
  125.     pCmdUI->SetCheck ((pCmdUI->m_nID - ID_WIDTH_VTHIN) == m_nWidth);
  126. }
  127.  
  128. void CPaintDoc::OnUpdateColorUI (CCmdUI* pCmdUI)
  129. {
  130.     pCmdUI->SetCheck ((pCmdUI->m_nID - ID_COLOR_BLACK) == m_nColor);
  131. }
  132.